/*
 * Repositoriobd.java
 *
 * Created on 17 de Agosto de 2008, 13:09
 */

package scil.dados;

/**
 *
 * @author jamesson
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

public class Repositoriobd {
    
    /** Creates a new instance of Repositoriobd */

    
    public Repositoriobd() 
    { 
     /*   try{   
         Class.forName("oracle.jdbc.driver.OracleDriver");   
         conexao = DriverManager.getConnection("jdbc:oracle:thin:@localhost:PORTA:8080","equipe17","equipe172008");   
        }catch(ClassNotFoundException cnf){   
            cnf.printStackTrace();   
        }   
        catch(SQLException sql){   
            sql.printStackTrace();   
        }   */
    }
    
    private static final String DRIVER = "oracle.jdbc.OracleDriver";
    private static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
    private static final String USUARIO = "equipe17";
    private static final String SENHA = "equipe172008";
    /**
	 * Utilizado para comandos SQL de update, insert ou delete
	 * 
	 * @param sql
	 */
	protected void executarSQL(String sql) {
		try {
			System.out.println("Comando Executado: " + sql);
			createStatement().executeUpdate(sql);
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

	/**
	 * Utilizado para executar comandos SQL de consulta (Select) 
	 * 
	 * @param sql
	 * @return
	 */
	protected ResultSet executarQuery(String sql) {
		try {
			System.out.println("Consulta Executada: " + sql);
			return createStatement().executeQuery(sql);
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
	protected Connection getConnection() {
		try {
			Class.forName(DRIVER);
                        return DriverManager.getConnection(URL, USUARIO, SENHA);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	protected void close(Connection connection) {
		if (connection != null) {
			try {
				connection. close();
			} catch (SQLException e) {
				throw new RuntimeException(e.getMessage()); 
			}
		}
	}
	protected void commitOff( Connection connection) {
		if (connection != null) {
			try {
				connection.setAutoCommit(false);
			} catch (SQLException e) {
				throw new RuntimeException(e.getMessage());
			}
		}
	}
	protected void commitOn( Connection connection) {
		if (connection != null) {
			try {
				connection.setAutoCommit(true);
			} catch (SQLException e) {
				throw new RuntimeException(e.getMessage());
			}
		}
	}
       	protected void commitDados( Connection connection) {
		if (connection != null) {
			try {
				connection.commit();
			} catch (SQLException e) {
				throw new RuntimeException(e.getMessage());
			}
		}
	}
	protected void close(Statement statement) {
		if (statement != null) {
			try {
				statement.close();
				close(statement.getConnection());
			} catch (SQLException e) {
				throw new RuntimeException(e.getMessage());
			}
		}
	}

	protected void close(ResultSet resultSet) {
		if (resultSet != null) {
			try {
				//close(resultSet.getStatement());
				resultSet.close();
			} catch (SQLException e) {
				throw new RuntimeException(e.getMessage());
			}
		}
	}
	
	protected Statement createStatement() {
		try {
			return getConnection().createStatement();
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
	
	protected PreparedStatement prepareStatement(String sql) {
		try {
			return getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

}
